package com.cakebackoffice;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.cakebackoffice.Model.Address;
import com.cakebackoffice.Model.Place;
import com.cakebackoffice.Model.Store;


public class AdminStoreActivity extends Activity {

    private Dialog dialog;
    private Store selectedStore;
    static final int CREATE_EDIT_NAME = 1;
    static final int CREATE_EDIT_TIME = 2;
    static final int CREATE_EDIT_ADRESS = 3;
    static final int ADD_STORE = 4;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.admin_store_fragment);


    }

    public void onStoreSelected(Store store) {
        AdminStoreDetailFragment viewer = (AdminStoreDetailFragment) getFragmentManager().findFragmentById(R.id.store_detail_fragment);

        if (viewer == null || !viewer.isInLayout()) {
            Intent detailIntent = new Intent(getApplicationContext(), AdminStoreDetailFragment.class);
            startActivity(detailIntent);
        } else {
            viewer.displayStore(store);
        }

        selectedStore = store;
    }

    protected Dialog onCreateDialog(int id) {

        switch (id) {
            case CREATE_EDIT_NAME:

                dialog = new Dialog(AdminStoreActivity.this);
                dialog.setContentView(R.layout.admin_store_change_property_dialog);

                dialog.setTitle("Edit Name");
                EditText editText = (EditText) dialog.findViewById(R.id.store_dialog_property_edittext);
                editText.setText(selectedStore.getName());
                ((dialog.findViewById(R.id.catalog_dialog_category_save_button))).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        AdminStoreActivity.this.dialogChangeName();
                    }
                });
                ((dialog.findViewById(R.id.button_save_cancel))).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();
                        removeDialog(CREATE_EDIT_NAME);
                    }
                });


                break;
            case ADD_STORE:

                dialog = new Dialog(AdminStoreActivity.this);
                dialog.setContentView(R.layout.admin_store_add_store_dialog);

                dialog.setTitle("Add Store");

                ((dialog.findViewById(R.id.catalog_dialog_category_save_button))).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        AdminStoreActivity.this.dialogAddStore();
                    }
                });
                ((dialog.findViewById(R.id.button_save_cancel))).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();
                        removeDialog(CREATE_EDIT_NAME);
                    }
                });


                break;
            case CREATE_EDIT_ADRESS:

                dialog = new Dialog(AdminStoreActivity.this);
                dialog.setContentView(R.layout.admin_store_adress_dialog);

                dialog.setTitle("Edit Adress");
                EditText editTextLigne1 = (EditText) dialog.findViewById(R.id.admin_store_edittext_adress_ligne1);
                EditText editTextLigne2 = (EditText) dialog.findViewById(R.id.admin_store_edittext_adress_ligne2);
                EditText editTextLigne3 = (EditText) dialog.findViewById(R.id.admin_store_edittext_adress_ligne3);
                EditText editTextLigne4 = (EditText) dialog.findViewById(R.id.admin_store_edittext_adress_ligne4);
                EditText editTextCity = (EditText) dialog.findViewById(R.id.admin_store_edittext_adress_city);
                EditText editTextPostalCode = (EditText) dialog.findViewById(R.id.admin_store_edittext_adress_postalcode);

                try {
                    editTextLigne1.setText(selectedStore.getPlace().getAddress().getLine1());
                    editTextLigne2.setText(selectedStore.getPlace().getAddress().getLine2());
                    editTextLigne3.setText(selectedStore.getPlace().getAddress().getLine3());
                    editTextLigne4.setText(selectedStore.getPlace().getAddress().getLine4());
                    editTextCity.setText(selectedStore.getPlace().getAddress().getCity());
                    editTextPostalCode.setText(selectedStore.getPlace().getAddress().getPostalCode());
                } catch (Exception e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }


                ((dialog.findViewById(R.id.button_save_Adresse))).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        AdminStoreActivity.this.dialogChangeAdress();
                    }
                });
                ((dialog.findViewById(R.id.button_save_cancel))).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();
                        removeDialog(CREATE_EDIT_ADRESS);
                    }
                });


                break;
            case CREATE_EDIT_TIME:

                dialog = new Dialog(AdminStoreActivity.this);
                dialog.setContentView(R.layout.admin_store_change_property_dialog);

                dialog.setTitle("Edit Time");
                EditText editText2 = (EditText) dialog.findViewById(R.id.store_dialog_property_edittext);
                editText2.setText(Float.toString(selectedStore.getEstimatedServiceTime()));
                ((dialog.findViewById(R.id.catalog_dialog_category_save_button))).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        AdminStoreActivity.this.dialogChangeTime();
                    }
                });
                ((dialog.findViewById(R.id.button_save_cancel))).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();
                        removeDialog(CREATE_EDIT_NAME);
                    }
                });


                break;
            default:
                dialog = null;
        }
        return dialog;
    }

    public void openDialogEditName() {
        showDialog(CREATE_EDIT_NAME);
    }

    public void openDialogEditTime() {
        showDialog(CREATE_EDIT_TIME);
    }

    public void openDialogEditAdress() {
        showDialog(CREATE_EDIT_ADRESS);
    }

    public void openDialogAddStore() {
        showDialog(ADD_STORE);
    }


    // Changement du nom
    public void dialogChangeName() {
        EditText editText = (EditText) dialog.findViewById(R.id.store_dialog_property_edittext);
        selectedStore.setName(editText.getText().toString());
        selectedStore.saveToDatabase(getBaseContext());
        SyncTaskManager.updateStoreOnServer(selectedStore);
        dialog.dismiss();
        removeDialog(CREATE_EDIT_NAME);
        onStoreSelected(selectedStore);
    }

    // Changement du temps
    public void dialogChangeTime() {
        EditText editText = (EditText) dialog.findViewById(R.id.store_dialog_property_edittext);
        try {
            selectedStore.setEstimatedServiceTime(Float.valueOf(editText.getText().toString()));
            selectedStore.saveToDatabase(getBaseContext());
            SyncTaskManager.updateStoreOnServer(selectedStore);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

        dialog.dismiss();
        removeDialog(CREATE_EDIT_TIME);
        onStoreSelected(selectedStore);
    }

    // Changement d'adresse
    public void dialogChangeAdress() {

        Address address = new Address();

        EditText editTextLigne1 = (EditText) dialog.findViewById(R.id.admin_store_edittext_adress_ligne1);
        EditText editTextLigne2 = (EditText) dialog.findViewById(R.id.admin_store_edittext_adress_ligne2);
        EditText editTextLigne3 = (EditText) dialog.findViewById(R.id.admin_store_edittext_adress_ligne3);
        EditText editTextLigne4 = (EditText) dialog.findViewById(R.id.admin_store_edittext_adress_ligne4);
        EditText editTextCity = (EditText) dialog.findViewById(R.id.admin_store_edittext_adress_city);
        EditText editTextPostalCode = (EditText) dialog.findViewById(R.id.admin_store_edittext_adress_postalcode);

        address.setCountry("France"); // A changer éventuellement dans une future version...
        address.setLine1(editTextLigne1.getText().toString());
        address.setLine2(editTextLigne2.getText().toString());
        address.setLine3(editTextLigne3.getText().toString());
        address.setLine4(editTextLigne4.getText().toString());
        address.setCity(editTextCity.getText().toString());
        address.setPostalCode(editTextPostalCode.getText().toString());

        Place place = new Place(address);
        place.saveToDatabase(getBaseContext());

        selectedStore = Store.getOne(getBaseContext(), selectedStore.getIdDB());
        selectedStore.setPlace(place);
        selectedStore.saveToDatabase(getBaseContext());

        SyncTaskManager.updateStoreOnServer(selectedStore);

        dialog.dismiss();
        removeDialog(CREATE_EDIT_ADRESS);
        onStoreSelected(selectedStore);

    }

    public void dialogAddStore() {
        EditText editText = (EditText) dialog.findViewById(R.id.store_dialog_edit_name);

        final Store store = new Store(editText.getText().toString(), 0f, null);

        store.saveToDatabase(getBaseContext());

        SyncTaskManager.insertStoreOnServer(getBaseContext(), store);

        // On refresh l'activity:
        Intent intent = new Intent(this, AdminStoreActivity.class);
        startActivity(intent);


        dialog.dismiss();
        removeDialog(ADD_STORE);
        finish();
    }

    public void setLocalStore() {

        try {
            Store.setLocalStore(getBaseContext(), selectedStore);
            Toast toast = Toast.makeText(getBaseContext(), "Store saved as local store.", Toast.LENGTH_SHORT);
            toast.show();
            onStoreSelected(selectedStore);
        } catch (Exception e) {
            e.printStackTrace();
            Toast toast = Toast.makeText(getBaseContext(), "An error as occured, contact provider", Toast.LENGTH_SHORT);
            toast.show();
        }

    }


}
